<?php
/**
 * admin888 <doan.hut@gmail.com>
 * Enter description : thao tac cho thu da gui
 * @author admin888
 *
 */
class Model_Thudagui extends Zend_Db_Table
{	
	protected $_gt = 'thu_gui';
	protected $_dk = 'dinh_kem';
	protected $_tv = 'thanhvien';
	
	
	public function __construct()
	{
		$this->_db = Zend_Registry::get('connectDb');
	}
	/**
	 * 
	 *admin888 <doan.hut@gmail.com>
	 * Enter description :Lay toan bo thu da gui theo nguoi dang nhap
	 * @param unknown_type $uid
	 * @param unknown_type $thongso
	 */
	public function getallthudagui($uid,$thongso)
	{
		$sql = $this->_db->select()
					->from($this->_gt)
					->join($this->_tv, $this->_tv.'.uid = '.$this->_gt.'.uid','name' )
					->where($this->_gt.'.uid = ?',$uid )
					->where($this->_gt.'.da_gui= ?',$thongso)
					->order($this->_gt.'.id_tg  DESC');
		//echo  $sql;			
		$query = $this->_db->fetchAll($sql);
	 return $query;				
	
	}
	/**
	 * 
	 *admin888 <doan.hut@gmail.com>
	 * Enter description :Lay toan bo thu da gui theo nguoi dang nhap
	 * @param unknown_type $uid
	 * @param unknown_type $thongso
	 */
	public function getallthudaguidb($uid,$thongso)
	{
		$sql = $this->_db->select()
					->from($this->_gt)
					->join($this->_tv, $this->_tv.'.uid = '.$this->_gt.'.uid','name' )
					->where($this->_gt.'.uid = ?',$uid )
					->where($this->_gt.'.da_gui= ?',$thongso)
					->order($this->_gt.'.id_tg  DESC')
					->limit(15);
		//echo  $sql;			
		$query = $this->_db->fetchAll($sql);
	 return $query;				
	
	}	
	/**
	 * 
	 *admin888 <doan.hut@gmail.com>
	 * Enter description :
	 * @param unknown_type $uid
	 * @param unknown_type $tukhoa
	 */
	public function getallthudaguitimkiem($uid,$thongso,$tukhoa)
	{
		$tukhoatk = trim($tukhoa);
		$sql = $this->_db->select()
					->from($this->_gt)
					->join($this->_tv, $this->_tv.'.uid = '.$this->_gt.'.uid','name' )
					->where($this->_gt.'.uid = ?',$uid )
					->where($this->_gt.'.da_gui= ?',$thongso)
					->where($this->_gt.'.tieu_de LIKE ?',$tukhoatk.'%')
					->order($this->_gt.'.id_tg  DESC');
		//echo  $sql;			
		$query = $this->_db->fetchAll($sql);
	 return $query;			
	}
	/**
	 *admin888 <doan.hut@gmail.com>
	 * Enter description :Xoa thu da gui 
	 * @param unknown_type $uid
	 * @param unknown_type $idt
	 */
	public function xoathudagui($uid,$idt)
	{
		$where = "id_tg = ".$idt;
		$xoa = $this->_db->delete($this->_gt,$where);
	 return $xoa;
	}
	/**
	 *admin888 <doan.hut@gmail.com>
	 * Enter description :Xoa fiel kem theo thu 
	 * @param unknown_type $idt
	 */
	public function xoafilekemtheothu($idt)
	{
		$where = 'id_t ='.$idt;
		$xoafile = $this->_db->delete($this->_dk,$where);
	 return $xoafile;
	}
	/**
	 * 
	 *admin888 <doan.hut@gmail.com>
	 * Enter description : lay file da lu 
	 * @param unknown_type $idt
	 */
	
	public function getalllinkfile($idt)
	{
		$slq = $this->_db->select()
						 ->from($this->_dk)
						 ->where($this->_dk.'.id_t =?',$idt);
						 
		$query = $this->_db->fetchAll($slq);
	return $query;					 		
	}
	/**
	 * 
	 *admin888 <doan.hut@gmail.com>
	 * Enter description : Xoa file trong thu muc 
	 * @param unknown_type $link
	 */
	public function xoafilecuathumuc($link)
	{
		
		//echo  $linkhinh;
		$xoa = unlink($link);		
	}
	/**
	 * 
	 *admin888 <doan.hut@gmail.com>
	 * Enter description :get thu theo id thu 
	 * @param unknown_type $idthu
	 */
	public function getthuid($idthu)
	{
		$sql = $this->_db->select()
						 ->from($this->_gt)
						 ->where($this->_gt.'.id_tg =?',$idthu);
		$query = $this->_db->fetchRow($sql);
	return $query;				 
	}
	/**
	 * 
	 *admin888 <doan.hut@gmail.com>
	 * Enter description : get dinh kem theo thu duoi dang json 
	 * @param unknown_type $idthu
	 */
	public function getdinhkem($idthu)
	{
		$sql = $this->_db->select()
						 ->from($this->_dk)
						 ->where($this->_dk.'.id_t = ?',$idthu);
		$query = $this->_db->fetchAll($sql);
	 return $query;			 
	}
	/**
	 * 
	 *admin888 <doan.hut@gmail.com>
	 * Enter description : Dem so thu
	 * @param unknown_type $uid
	 * @param unknown_type $thongso
	 */
	public function demthu($uid,$thongso)
	{
			$sql = $this->_db->select()
							 ->from($this->_gt,array('COUNT(*) AS tongthu'))
							 ->where($this->_gt.'.uid = ?',$uid)
							 ->where ($this->_gt.'.da_gui =? ',$thongso);
			$query = $this->_db->fetchOne($sql);
		return $query;					 	
		
	}
	
	
}